using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Imp : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] values = base.Request.QueryString["ID"].Replace("'", "").Replace(";", "").Split(new char[] { '_' });
        if (values.Length != 2)
        {
            base.Response.Write("This content do not exists.");
            return;
        }
        string strImagePath = ConfigurationManager.AppSettings["ImageFolder"];
        string strGifPath = ConfigurationManager.AppSettings["GifFolder"];
        string strMidiPath = ConfigurationManager.AppSettings["MidiFolder"];
        string strVoicePath = ConfigurationManager.AppSettings["VoiceFolder"];
        string strMp3Path = ConfigurationManager.AppSettings["Mp3Folder"];

        string strMediaPath = "";
        string strMime = "";
        string strFileName = "";
        DataTable dt = TransInfoManager.SelectInfo(values[0], long.Parse(values[1]));

        if (dt.Rows.Count <= 0)
        {
            return;
        }
        switch (dt.Rows[0]["DataType"].ToString().ToUpper())
        {
            case "IMAGE":
                if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 1)
                    strMediaPath = strImagePath + @"\128x128\" + dt.Rows[0]["DataID"].ToString();
                
                else if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 2)
                    strMediaPath = strImagePath + @"\128x160\" + dt.Rows[0]["DataID"].ToString();
                
                else if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 3)
                    strMediaPath = strImagePath + @"\176x144\" + dt.Rows[0]["DataID"].ToString();
                
                else if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 4)
                    strMediaPath = strImagePath + @"\176x220\" + dt.Rows[0]["DataID"].ToString();

                else
                    strMediaPath = strImagePath + @"\240x320\" + dt.Rows[0]["DataID"].ToString();
                
                strMime = "image/jpeg";
                strFileName = dt.Rows[0]["DataID"].ToString() + ".jpg";
                goto GoTo;

            case "GIF":
                strMediaPath = strGifPath + dt.Rows[0]["DataID"].ToString();
                strMime = "image/gif";
                strFileName = dt.Rows[0]["DataID"].ToString() + ".gif";
                goto GoTo;

            case "MIDI":
                strMediaPath = strMidiPath + dt.Rows[0]["DataName"].ToString();
                strMime = "audio/mid";
                strFileName = dt.Rows[0]["DataName"].ToString() + ".mid";
                break;

            case "MP3":
                if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 1)
                    strMediaPath = strMp3Path + @"\128kbps\" + dt.Rows[0]["DataName"].ToString();
                else
                    strMediaPath = strMp3Path + @"\64kbps\" + dt.Rows[0]["DataName"].ToString();
                
                strMime = "video/mpeg";
                strFileName = dt.Rows[0]["DataName"].ToString() + ".mp3";
                goto GoTo;

            case "VOICE":
                if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 1)
                {
                    strMediaPath = strVoicePath + @"\Voice\Amr\" + dt.Rows[0]["DataName"].ToString();
                    strMime = "audio/amr";
                    strFileName = dt.Rows[0]["DataName"].ToString() + ".amr";
                }

                else if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 2)
                {
                    strMediaPath = strVoicePath + @"\Voice\Midi\" + dt.Rows[0]["DataName"].ToString();
                    strMime = "audio/mid";
                    strFileName = dt.Rows[0]["DataName"].ToString() + ".mid";
                }

                else if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 3)
                {
                    strMediaPath = strVoicePath + @"\Voice\Mmf\" + dt.Rows[0]["DataName"].ToString();
                    strMime = "application/vnd.smaf";
                    strFileName = dt.Rows[0]["DataName"].ToString() + ".mmf";
                }

                else if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 4)
                {
                    strMediaPath = strVoicePath + @"\Voice\Swf\" + dt.Rows[0]["DataName"].ToString();
                    strMime = "application/x-shockwave-flash";
                    strFileName = dt.Rows[0]["DataName"].ToString() + ".swf";
                }

                else if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 5)
                {
                    strMediaPath = strVoicePath + @"\Voice\Wav\" + dt.Rows[0]["DataName"].ToString();
                    strMime = "audio/x-wav";
                    strFileName = dt.Rows[0]["DataName"].ToString() + ".wav";
                }

                else if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 6)
                {
                    strMediaPath = strVoicePath + @"\Voice\Mp3\128kbps\" + dt.Rows[0]["DataName"].ToString();
                    strMime = "video/mpeg";
                    strFileName = dt.Rows[0]["DataName"].ToString() + ".mp3";
                }

                else if (int.Parse(dt.Rows[0]["SizeID"].ToString()) == 7)
                {
                    strMediaPath = strVoicePath + @"\Voice\Mp3\64kbps\" + dt.Rows[0]["DataName"].ToString();
                    strMime = "video/mpeg";
                    strFileName = dt.Rows[0]["DataName"].ToString() + ".mp3";
                }

                else
                {
                    strMediaPath = strVoicePath + @"\Voice\Amr\" + dt.Rows[0]["DataName"].ToString();
                    strMime = "audio/amr";
                    strFileName = dt.Rows[0]["DataName"].ToString() + ".amr";
                }
                goto GoTo;
        }

    GoTo:
        if (File.Exists(strMediaPath))
        {
            FileStream stream = new FileStream(strMediaPath, FileMode.Open);
            int count = Convert.ToInt32(stream.Length);
            byte[] buffer = new byte[count];
            stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
            stream.Close();
            base.Response.Clear();
            base.Response.ContentType = strMime;
            int length = buffer.Length;
            base.Response.AppendHeader("Content-Length", length.ToString());
            base.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", strFileName));
            base.Response.OutputStream.Write(buffer, 0, count);
            base.Response.Flush();
            base.Response.Close();
        }
        else
        {
            base.Response.Write("This content do not exists.");
        }
    }
}
